package com.wd.live.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.library.AutoFlowLayout;
import com.example.library.FlowAdapter;
import com.wd.live.R;
import com.wd.live.base.BaseFragment;
import com.wd.live.base.BaseViewModel;
import com.wd.live.bean.SignBean;
import com.wd.live.databinding.SexFragmentBinding;
import com.wd.live.databinding.SiginFragmentBinding;

import com.wd.live.view.activity.MainActivity;
import com.wd.live.viewmodel.SiginModel;

import java.util.ArrayList;

import java.util.List;


public class SiginFragment extends BaseFragment<SiginModel, SiginFragmentBinding> {



   public static List<String> newList = new ArrayList<String>();
    List<String> list=new ArrayList<>();
    @Override
    protected void initData() {
        databind.jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
            }
        });


        viewmodel.getSignData().observe(this, new Observer<SignBean>() {
            @Override
            public void onChanged(SignBean signBean) {
                List<SignBean.ResultBean> result = signBean.getResult();
                databind.FlowLayout.setAdapter(new FlowAdapter<SignBean.ResultBean>(result) {
                    @Override
                    public View getView(int i) {
                        View inflate = View.inflate(getContext(), R.layout.flowlayout_item,null);
                        TextView viewById = inflate.findViewById(R.id.Searchname);
                        viewById.setText(result.get(i).getName());
                        viewById.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                String name = signBean.getResult().get(i).getName();
                                viewById.setBackgroundResource(R.drawable.biankuang_cheack);
                                Log.v("signname",name);
                                list.add(name);
                                for(int i = 0; i < list.size(); i++ ){
                                    //判断新集合是否包含有，如果不包含有，则存入新集合中
                                    boolean isContains = newList.contains(list.get(i));
                                    if(!isContains){
                                        newList.add(list.get(i));
                                    }
                                }
                                list.clear();
                                //把新集合元素添加至List集合
                                list.addAll(newList);
                                databind.FlowLayoutselect.clearViews();
                                databind.FlowLayoutselect.setAdapter(new FlowAdapter<String>(list){
                                    @Override
                                    public View getView(int i) {

                                        View inflate = View.inflate(getContext(),R.layout.flowlayoutselct_item,null);
                                        TextView viewById = inflate.findViewById(R.id.Searchname);
                                        Log.v("newlist",newList+"");
                                        viewById.setText(newList.get(i));

                                        return inflate;
                                    }
                                });
                            }
                        });
                        return inflate;

                    }
                });

            }
        });
        databind.gointo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),MainActivity.class));
                getActivity().finish();
            }
        });

    }
    @Override
    protected int initLayout() {
        return R.layout.sigin_fragment;
    }

    @Override
    public void onChanged(Object o) {

    }
}